<?php  
	/**
	* 前台职位信息
	*/
	class PosInfoViewModel extends ViewModel
	{
		public $table = 'company_position';

		public $view = array(
				'company_baseinfo'=>array(
					'type'	=>	INNER_JOIN,
					'on' => 'company_position.user_uid=company_baseinfo.user_uid'
					),
				'company_otherinfo' =>array(
					'type'=>INNER_JOIN,
					'on'=>'company_position.user_uid = company_otherinfo.user_uid'
					)

			);
		private $where = null;

		public function __init()
		{
			$this->where = array(
					'showstate'		=>	1,
					'verifystate'	=>	2,
					'endtime'		=>	array('gt'=>time())
					);
		}

		#前台首页显示最新职位
		public function get_data($field=null,$limit=null)
		{
			$newPos = S('newPos',false,0,array('dir'=>CACHE_PATH));

			if(!$newPos){
				$order = 'sendtime DESC,freshtime DESC';
				$newPos = $this->field($field)->where($this->where)->order($order)->limit($limit)->all();
				S('newPos',$newPos,30,array('dir'=>CACHE_PATH));
			}


			return $newPos;
		}

		public function get_data_all($field=null,$limit=null)
		{
			$newPosAll = S('newPosAll',false,0,array('dir'=>CACHE_PATH));

			if(!$newPosAll){
				$order = 'sendtime DESC,freshtime DESC';
				$newPosAll = $this->field($field)->where($this->where)->order($order)->limit($limit)->all();
				S('newPosAll',$newPosAll,30,array('dir'=>CACHE_PATH));
			}


			return $newPosAll;
		}

		#搜索筛选数据
		public function filter_data($field=null,$page=10)
		{
			$city_one = Q('get.city_one',0,'intval');
			$city_second = Q('get.city_second',0,'intval');

			$cityModel = K('City');
			#如果是二级城市直接返回
			if ($city_second) {
				$city_cityid = $city_second;
			}else{
				#如果$city_one 不等于0
				if ($city_one) {
					#如果是一级城市，返回所有的一级城市里的内容以及该城市的子孙城市内容
					$city_cityid = $cityModel->get_city_id($city_one);
				}else{#等于0的情况
					$city_cityid = array_keys($cityModel->city);
				}
			}

			#职位处理逻辑和city的一样
			$pos_one = Q('get.pos_one',0,'intval');
			$pos_second = Q('get.pos_second',0,'intval');
			$posModel = K('PosCate');
			if ($pos_second) {
				$pos_id = $pos_second;
			}else{
				if ($pos_one) {
					$pos_id = $posModel->get_pos_id($pos_one);
				}else{
					$pos_id = array_keys($posModel->pos);
				}
			}


			$where = $this->where;
			if(isset($city_cityid))$where[C('DB_PREFIX') . 'company_position.city_cityid'] = $city_cityid;
			$url = urldecode(__URL__);	
			if (isset($pos_id))$where[C('DB_PREFIX') . 'company_position.cate_posid'] = $pos_id;
			if ($keyword = Q('get.keyword')) {
				switch (Q('get.type',1,'intval')) {
					case '1':
						$where[] = 'positionname like "%'.$keyword.'%"';
						break;
					case '2':
						$where[] = 'companyname like "%'.$keyword.'%"';
						break;
					case '3':
						$where[] = 'description like "%'.$keyword.'%"';
						break;
				}
			}else{
				$url = str_replace('&keyword=','',$url);
			}
		
			if ($quality = Q('get.quality')){
				$where['quality'] = $quality;
				$url = trim(substr($url,0,stripos($url,'quality[]')),'&');
				foreach ($quality as $key => $value) {
					$url .= '&quality'.$key .'='.$value;
				}
			}else{
				unset($_GET['quality']);

			} 
			if ($quality0 = Q('get.quality0')) {
				$where['quality'][] = $quality0;
			}
			if ($quality1 = Q('get.quality1')) {
				$where['quality'][] = $quality1;
			}
			if ($quality2 = Q('get.quality2')) {
				$where['quality'][] = $quality2;
			}
			// p($where);
			if(isset($where['quality']))$_GET['quality'] = $where['quality'];

			// p($where);
			if(!$field) $field = 'hd_job_company_baseinfo.*,hd_job_company_otherinfo.*,hd_job_company_position.*';
			$total = $this->where($where)->count();
			// p($where);
			$page = new Page($total,8,6,'','',$url);
			$data = $this->field($field)->where($where)->order('sendtime DESC,freshtime DESC')->all($page->limit());
			
			
			if (!$data) return $data;
			// $cityname=$cityModel->city[1];
			foreach ($data as $key => &$value) {
				$value['cityname'] = $cityModel->city[$value['city_cityid']];
				switch ($value['sex']) {
					case 0:
						$value['sex'] = '不限';
						break;
					case 1:
						$value['sex'] = '男';
						break;
					case 2:
						$value['sex'] = '女';
						break;
				}
				$value['endtime'] = date('Y-m-d',$value['endtime']);
			}
			$data['page'] = $page->show();
			// p($data);
			return $data;

		}


		public function get_all($where)
		{
			$total = $this->count();
			$page = new page($total);
			$limit = $page -> limit();
			if($where) $where = array_merge($this->where,$where);
			p($where);
			$data  = $this->where($where)->order('sendtime DESC,endtime DESC,cpid DESC')->all($limit);
			$data['page'] = $page->show();
			return $data;
		}

		public function get_one($where)
		{
			return $this->where($where)->find();
		}

	}
	
?>